package com.bright.hxj.cwduijie.service.impl;

import com.bright.hxj.cwduijie.enums.DataApi;
import com.bright.hxj.cwduijie.enums.SJJHBZ;
import com.bright.hxj.cwduijie.pojo.bo.ApiDataCollection;
import com.bright.hxj.cwduijie.pojo.po.Contract;
import com.bright.hxj.cwduijie.pojo.query.DuijieQuery;
import com.bright.hxj.cwduijie.repository.ContractAssetsRepository;
import com.bright.hxj.cwduijie.repository.ContractPaymentRepository;
import com.bright.hxj.cwduijie.repository.ContractReceiptRepository;
import com.bright.hxj.cwduijie.repository.ContractRepository;
import com.bright.hxj.cwduijie.service.AbstractPerThousandApiService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.stereotype.Component;

import javax.persistence.criteria.Predicate;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.function.Function;

/**
 * @Author hxj
 * @Date 2024/7/29 14:43
 * @Description
 */

@Component
@RequiredArgsConstructor
@Slf4j
public class ApiContractServiceImpl extends AbstractPerThousandApiService<Contract> {

    private final ContractRepository contractRepository;
    private final ContractAssetsRepository contractAssetsRepository;
    private final ContractPaymentRepository contractPaymentRepository;
    private final ContractReceiptRepository contractReceiptRepository;

    @Override
    public JpaSpecificationExecutor getJpaSpecificationExecutor() {
        return contractRepository;
    }

    @Override
    public DataApi getDataApi() {
        return DataApi.CONTRACT;
    }

    @Override
    public Function<Contract, Integer> getIdFunc() {
        return Contract::getID;
    }

    @Override
    protected void getData(ApiDataCollection apiDataCollection, DuijieQuery duijieQuery) {
        List<Contract> contracts = getNeedDuijieData(duijieQuery);
        for (Contract contract : contracts) {
            contract.setASSET_LIST(contractAssetsRepository.findAll(getDataKeySpecification(contract.getDATA_KEY())));
            contract.setPAYMENT_SCHEDULE_LIST(contractPaymentRepository.findAll(getDataKeySpecification(contract.getDATA_KEY())));
            contract.setRECEIPT_SCHEDULE_LIST(contractReceiptRepository.findAll(getDataKeySpecification(contract.getDATA_KEY())));
        }
        apiDataCollection.setContracts(contracts);
    }

    private <T> Specification<T> getDataKeySpecification(String dataKey) {
        return (root, criteriaQuery, criteriaBuilder) -> {
            criteriaQuery.where(new Predicate[]{criteriaBuilder.equal(root.get("DATA_KEY"), dataKey)});
            return criteriaQuery.getRestriction();
        };
    }

    @Override
    public Function<Contract, String> getDataKeyFunc() {
        return Contract::getDATA_KEY;
    }

    @Override
    public Function<ApiDataCollection, List<Contract>> getDataFunc() {
        return ApiDataCollection::getContracts;
    }

//    @Override
//    protected List<Contract> getNeedDeleteData(DuijieQuery duijieQuery) {
//        List<Contract> contracts = contractRepository.getAllByTYSHXYDMAndSjjhbzAndOpType(duijieQuery.getCreditCode(), SJJHBZ.WAITING.getValue(), OpType.DEL.getValue());
//        contracts.addAll(contractRepository.getAllByTYSHXYDMAndSjjhbzAndOpType(duijieQuery.getCreditCode(), SJJHBZ.ERROR.getValue(), OpType.DEL.getValue()));
//        return contracts;
//    }

    @Override
    public DataApi getDeleteDataApi() {
        return DataApi.DELETE_CONTRACT;
    }
}
